﻿///<reference path="/Scripts/JQuery/jquery-1.8.3-vsdoc.js" />
///<reference path="/ScriptsG/RegisterDynamic.js" />
///<reference path="/Scripts/JSonModels/JSonModelsMenuEdition1.js" />

$.fn.DocReady = function () {
    //Add call to execute when document is ready
    $("#cmdSubmit").button();
    $("#form0").on("submit",$.fn.BeforSubmitHandler);
    $("#UserToRegister_Username").on("focusout", $.fn.CheckUserNameAvailability);
    $("#UserToRegister_Email").on("focusout", $.fn.CheckEMailAvailability);
    $("#UserToRegister_ConfirmPassword").attr("data-val-equalto-other", "UserToRegister.Password");
}
$.fn.CheckUserNameAvailability = function () {
    CheckUserNameAvailability_JSonModel.username = $(this).val();
    if (CheckUserNameAvailability_JSonModel.username.length > 0) {
        var ajaxData = JSON.stringify(CheckUserNameAvailability_JSonModel);
        var url = "/api/CheckUserNameAvailability/" + $("#hidLanguage").val();
        $.ajax(
        {
            type: "POST",
            url: url,
            data: ajaxData,
            contentType: "application/json",
            async: false,
            success: $.fn.CheckUserNameAvailabilityResult,
            error: function (xhr, ajaxOptions, thrownError) {
                $.fn.jqDialogAlert(cap_DialogErrorTitle, (xhr.responseText));
            }
        });
    }
}
$.fn.CheckEMailAvailability = function () {
    CheckEMailAvailability_JSonModel.emailaddress = $(this).val();
    if (CheckEMailAvailability_JSonModel.emailaddress.length > 0) {
        var ajaxData = JSON.stringify(CheckEMailAvailability_JSonModel);
        var url = "/api/CheckEMailAvailability/" + $("#hidLanguage").val();
        $.ajax(
        {
            type: "POST",
            url: url,
            data: ajaxData,
            contentType: "application/json",
            async: false,
            success: $.fn.CheckEMailAvailabilityResult,
            error: function (xhr, ajaxOptions, thrownError) {
                $.fn.jqDialogAlert(cap_DialogErrorTitle, (xhr.responseText));
            }
        });
    }
}
$.fn.CheckUserNameAvailabilityResult = function (a, b, c) {
    if (a.success) {
        $.fn.FieldValueNotAvailable("#UserToRegister_Username", a.isAvailable);
    }
}
$.fn.CheckEMailAvailabilityResult = function (a, b, c) {
    if (a.success) {
        $.fn.FieldValueNotAvailable("#UserToRegister_Email", a.isAvailable);
    }
}
$.fn.FieldValueNotAvailable = function (inputSelector, isAvailable) {
    if (isAvailable) {
        $(inputSelector).css("color", "green");
        $(inputSelector).removeClass("NotAvailable");
        $(inputSelector).attr("title", cap_Available);
    } else {
        $(inputSelector).css("color", "red");
        $(inputSelector).attr("title", cap_NotAvailable);
        $(inputSelector).addClass("NotAvailable");
        $(inputSelector).focus();
    }
}
$.fn.BeforSubmitHandler = function () {
    if ($(".NotAvailable").length > 0) {
        $(".NotAvailable")[0].focus();
        return false;
    }
}
